﻿//************************************************************************************************
// Copyright © 2014 Steven M. Cohn. All Rights Reserved.
// 
//************************************************************************************************

namespace River.Folderize.UI
{
	using System;
	using System.Windows;
	using System.Windows.Controls;
	using System.IO;
	using Forms = System.Windows.Forms;
	using System.Windows.Input;
	using System.Windows.Threading;

	
	/// <summary>
	/// Interaction logic for PropertiesPanel.xaml
	/// </summary>
	
	internal partial class PropertiesPanel : UserControl
	{

		private Folder folder;
		private Folder original;
		private ImageHelper.ImageType imageType;
		private bool ignoreEsc;


		public PropertiesPanel ()
		{
			InitializeComponent();
			ignoreEsc = false;
		}


		public event PropertiesEditedRoutedEventHandler Completed;



		// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
		// Public Methods
		// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

		public void Create (CustomFolder folder)
		{
			original = null;
			Prepare(folder, "New Folder",
				ImageHelper.ImageType.Folder, FolderController.EmptyIconRes);
		}

		public void Create (LibraryFolder folder)
		{
			original = null;
			Prepare(folder, "New Library",
				ImageHelper.ImageType.Library, LibraryController.EmptyIconRes);
		}
	
		public void Modify (CustomFolder folder)
		{
			original = new CustomFolder(folder);
			Prepare(folder, "Folder Properties",
				ImageHelper.ImageType.Folder, FolderController.EmptyIconRes);
		}

		public void Modify (LibraryFolder folder)
		{
			original = new LibraryFolder(folder);
			Prepare(folder, "Library Properties", 
				ImageHelper.ImageType.Library, LibraryController.EmptyIconRes);
		}


		private void Prepare (
			Folder folder, string title,
			ImageHelper.ImageType imageType, string emptyIconRes)
		{
			folder.BeginEdit();

			titleLabel.Content = title;
			this.imageType = imageType;

			if (String.IsNullOrEmpty(folder.ImageRes))
			{
				folder.ImageRes = emptyIconRes;
				folder.Image = ImageHelper.LoadIcon(folder.ImageRes, imageType);
			}

			DataContext = this.folder = folder;
			Visibility = Visibility.Visible;
		}


		private void VisibilityChanged (object sender, DependencyPropertyChangedEventArgs e)
		{
			if ((bool)e.NewValue)
			{
				Dispatcher.BeginInvoke(
					DispatcherPriority.ContextIdle,
					new Action(delegate()
					{
						folder = DataContext as Folder;

						pathBox.IsEnabled = pathButton.IsEnabled
							= (folder.Path == null)
							|| !folder.Path.Equals(FolderController.LibrariesID);

						errorLabel.Content = String.Empty;
						nameBox.Focus();
					})
				);
			}
		}

	
		// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
		// Actions
		// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

		private void ChangeIcon (object sender, RoutedEventArgs e)
		{
			string path;
			int index;

			if (ImageHelper.PickIcon(folder.ImageRes, out path, out index, imageType))
			{
				folder.ImageRes = path + "," + index.ToString();
				folder.Image = ImageHelper.LoadIcon(path, index, imageType);
			}
		}


		private void ChangedPath (object sender, RoutedEventArgs e)
		{
			string path = pathBox.Text.Trim();
			if (path.Length > 0)
			{
				string name = (folder.Name == null ? String.Empty : folder.Name.Trim());
				if (name.Length == 0)
				{
					folder.Name = Path.GetFileName(pathBox.Text);
				}
			}
		}

	
		private void ChoosePath (object sender, RoutedEventArgs e)
		{
			using (var dialog = new Forms.FolderBrowserDialog())
			{
				dialog.ShowNewFolderButton = true;

				if (!String.IsNullOrEmpty(folder.Path) && Directory.Exists(folder.Path))
				{					
					dialog.SelectedPath = folder.Path;
				}

				var result = dialog.ShowDialog();
				ignoreEsc = (result == Forms.DialogResult.Cancel) && Keyboard.IsKeyDown(Key.Escape);

				if (result == Forms.DialogResult.OK)
				{
					folder.Path = dialog.SelectedPath;

					string name = (folder.Name == null ? String.Empty : folder.Name.Trim());
					if (name.Length == 0)
					{
						folder.Name = Path.GetFileNameWithoutExtension(folder.Path);
					}
				}
			}
		}


		private void ClickOK (object sender, RoutedEventArgs e)
		{
			if ((folder.Name == null) || (folder.Name.Trim().Length == 0))
			{
				errorLabel.Content = "Name is required";
				nameBox.Focus();
				return;
			}

			if (pathBox.IsEnabled)
			{
				if ((folder.Path == null) || (folder.Path.Trim().Length == 0))
				{
					errorLabel.Content = "Path is required";
					pathBox.Focus();
					return;
				}
				else if (!Directory.Exists(folder.Path))
				{
					errorLabel.Content = "Path does not exist";
					pathBox.Focus();
					return;
				}
			}

			folder.EndEdit();

			this.Visibility = Visibility.Hidden;

			if (Completed != null)
			{
				var args = new PropertiesEditedRoutedEventArgs(folder, original);
				Completed(this, args);
			}
		}


		private void ClickCancel (object sender, RoutedEventArgs e)
		{
			folder.CancelEdit();
			this.Visibility = Visibility.Hidden;
		}


		private void CheckEscape (object sender, KeyEventArgs e)
		{
			if (e.Key == Key.Escape)
			{
				if (ignoreEsc)
				{
					e.Handled = true;
					ignoreEsc = false;
					return;
				}

				e.Handled = true;
				ClickCancel(sender, e);
			}
		}
	}
}
